/*jshint esversion: 9 */
import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store';

import notfount from "@/views/notFount.vue";
import indexLayout from '@/layout/index';
import home from '@/views/home';
import mine from '@/views/mine';
import login from '@/views/login';
import updatePassword from '@/views/updatePassword';
import findData from '@/views/findData';

Vue.use(VueRouter);
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {  //修改错误提示
    return VueRouterPush.call(this, to).catch((err) => err);
};

const routes = [
    {
        path: '*',  //404页面
        name: 'Notfount',
        component: notfount,
        meta: {
            title: '没找到该页面',
            myKeepAlive:true,
        },
    },
    {
        path: '/m',  //跳转到手机页面
        redirect: 'home',
    },
    {
        path: '/',
        name:'IndexLayout',
        component: indexLayout,
        redirect: 'home',
        meta: {
            myKeepAlive:true,
        },
        children: [
            {
                path: 'home',
                name: 'Home',
                component:home,
                meta: {
                    myKeepAlive:true,
                    title: '数据查询',
                }
            },
            {
                path: 'mine',
                name: 'Mine',
                component:mine,
                meta: {
                    myKeepAlive:true,
                    title: '个人中心',
                }
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        component:login,
        meta: {
            myKeepAlive:false,
            title: '登录',
        }
    },
    {
        path: '/updatePassword',
        name: 'UpdatePassword',
        component: updatePassword,
        meta: {
            myKeepAlive:false,
            title: '修改密码',
        }
    },
    {
        path: '/findData/:customerId/:userId',
        name: 'FindData',
        component: findData,
        meta: {
            myKeepAlive:false,
            title: '查询验证码',
        }
    },
];

const router = new VueRouter({
    mode: 'history',
    base: '/',
    routes: routes,
});

router.beforeEach((to, from, next) => {
    if(to.path != '/login' && !store.state.userModule.user.token){
        next('/login');
    }else{
        next();
    }
});

export default router;